package com.example.studyapp.data

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.children
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.example.studyapp.R
import com.example.studyapp.server_queues
import com.example.studyapp.server_url
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject
import org.w3c.dom.Text

//列表选项类
open class myRecyclerAdapter1(val textList: List<String>,val mylist: List<String>):
    RecyclerView.Adapter<myRecyclerAdapter1.MyViewHolder1>(){
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder1 {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.itemview, parent, false)
        return MyViewHolder1(view)
    }
    override fun getItemCount(): Int=textList.size?:0
    override fun onBindViewHolder(holder: MyViewHolder1, position: Int) {
        val text1=holder.title.getChildAt(0) as TextView
        val text2=holder.title.getChildAt(1) as TextView
        text1.text=textList[position]
        text2.text=mylist[position]
        holder.itemView.setOnClickListener {
            onItemClick(holder,position)
        }
    }
    //某项被单击
    open fun onItemClick(holder: MyViewHolder1, position: Int){

    }
    class MyViewHolder1(itemView: View): RecyclerView.ViewHolder(itemView){
        val title: LinearLayout =itemView.findViewById(R.id.item_view)

    }

}

//列表选项类
open class myRecyclerAdapter(val textList: List<String>):
    RecyclerView.Adapter<myRecyclerAdapter.MyViewHolder>(){
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.choice_item, parent, false)
        return MyViewHolder(view)
    }
    override fun getItemCount(): Int=textList.size?:0
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.title.text=textList[position]
        holder.itemView.setOnClickListener {
            onItemClick(holder,position)
        }
    }
    //某项被单击
    open fun onItemClick(holder: MyViewHolder, position: Int){

    }
    class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val title: TextView =itemView.findViewById(R.id.mine_title)

    }
}

//使用json进行通讯
open class jsonInteraction(jsonobj: JSONObject) {
    init{
        val request=object : JsonObjectRequest(
            Request.Method.POST,
            server_url,
            null,
            Response.Listener { response ->//返回的数据
                //初始化列表
                myResposeListener(response)
            },
            Response.ErrorListener { err -> // 出错信息
                myResposeErrorListener(err)
            }
        ) {
            override fun getBody(): ByteArray {//发送的数据
                return jsonobj.toString().toByteArray(
                )
            }
        }
        request.tag="volleyPost"
        server_queues.add(request)
    }
    open fun myResposeListener(response: JSONObject){  //执行代码

    }
    open fun myResposeErrorListener(err: VolleyError) {//出错代码

    }
}

